import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    // 初始化 全局token
    token: getToken() || null
  },
  mutations: {
    // token 存储方法
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    removeToken (state) {
      state.token = null
      removeToken()
    }
  },
  actions: {
    // 调用login 接口
    async loginAction (ctx, loginForm) {
      const token = await login(loginForm)
      ctx.commit('setToken', token)
    }
  }
}
